<template>
  <el-pagination
    v-if="total"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    :current-page="currentPage"
    :page-sizes="pageSizes"
    :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  />
</template>
<script>
// import name from "./name";
export default {
  // components: { name},
  props: {
    value: {
      type: [Array],
      default: () => {
        return [];
      }
    }
  },
  watch: {
    value: {
      handler(a) {
        // console.log(this.$cp(a));
        this.frist();
      },
      deep: true
    }
  },
  data() {
    return {
      total: 0,
      currentPage: 1,
      pageSizes: [10, 50, 100, 200, 500],
      pageSize: 10
    };
  },
  // computed() {return{}},
  created() {
    this.frist();
  },
  methods: {
    frist() {
      this.total = this.value.length;
      // this.currentPage = 1;
      this.sPage();
    },
    handleSizeChange(val) {
      // 改变每页显示数
      this.currentPage = 1;
      this.pageSize = val;
      this.sPage();
    },
    handleCurrentChange(val) {
      // 改变分页
      this.currentPage = val;
      this.sPage();
    },
    sPage() {
      let tabData = [];
      if (this.value.length) {
        let start = (this.currentPage - 1) * this.pageSize;
        let end = this.currentPage * this.pageSize;
        if (end > this.value.length) end = this.value.length;
        if (this.value.length < start && this.currentPage > 1) {
          this.currentPage--;
          this.sPage();
          return;
        }
        let i = start;
        let isitem = true;
        let tmp = {};
        let item = {};
        let j = 0;
        while (i < end) {
          tmp = this.value[i];
          if (isitem) {
            if (
              tmp.inType([
                "id",
                "depository",
                "project",
                "item",
                "process",
                "sum",
                "arrived",
                "use",
                "stock",
                "state"
              ])
            ) {
              if (!tmp.inType(["uid", "name", "model"])) {
                item = this.$gd.items.getItem({ id: tmp.item });
                j = this.$gd.stock.getItem({ id: tmp.id }, true);
                this.$gd.stock[j].uid = item.uid;
                this.$gd.stock[j].name = item.name;
                this.$gd.stock[j].model = item.model;
              }
            } else isitem = false;
          }

          tabData.push(tmp);
          i++;
        }

        // let tabData = this.value.slice(start, end);
        // console.log(this.$cp(tabData),this.currentPage);
      }
      this.$emit("change", tabData);
    }
  }
};
</script>
